package org.umlviewer.model;

import java.awt.Graphics;
import java.util.ArrayList;

/**
 *
 * @author Rafa
 */
public class UmlDiagram {

    private ArrayList<DrawElement> elements;
    //private ArrayList<DiagramElement> elements;
    //private ArrayList<Relationship> links;
    private String diagramName;
    //el tipo de diagrama que se instancia
    private String diagramType;
    
    public UmlDiagram()
    {
        elements = new ArrayList<DrawElement>();
        //links = new ArrayList<Relationship>();
    }

    public String getDiagramName() {
        return diagramName;
    }

    public void setDiagramName(String name) {
        this.diagramName = name;
    }
        
    
    public void paint(Graphics g)
    {
        for(DrawElement e : elements)        
            e.paint(g);                 
        //for(Relationship r : links)
          //  r.paint(g);
    }
    
    public void addElement(DiagramElement e)
    {
        this.elements.add(e);
    }
    
    public void addRelationship(Relationship r)
    {
        this.elements.add(r);
    }    
    
//    private boolean verificaCondicion(DiagramElement n, int x, int y) {
//       // return (x <= n.getPosX() + (2 * 10) && x >= n.getPosX() - 10) && (y <= n.getPosY() + (2 * 10) && y >= n.getPosY() - 10);
//        return ( (x >= n.getPosX() && x<= n.getPosX()+ n.getWidth() )
//                  && (y >= n.getPosY() && y<= n.getPosY() + n.getHeight()) );
//    }

    public DrawElement elementAtXY(int x, int y) {
        for (DrawElement n : elements) {
            if (n.contains( x, y)) {
                return n;
            }
        }
        return null;
    }          
    
    public DiagramElement getElementXY(int x, int y){
        for(DrawElement e: elements){
            if(e instanceof DiagramElement){
                if(e.contains(x, y))
                    return (DiagramElement)e;
            }
        }
        return null;
    }
    
    /*public Relationship relationshipAt(int x, int y){
        for(Relationship r : links){
            if(r.contains(x, y))
                return r;
        }
        return null;
    }*/
    
    public ArrayList<DiagramElement> getElements()
    {
        ArrayList<DiagramElement> delements = new ArrayList<DiagramElement>();
        for(DrawElement e : elements){
            if(e instanceof DiagramElement){
                DiagramElement de = (DiagramElement)e;
                delements.add(de);
            }
        }
        return delements;
    }
    
    public ArrayList<Relationship> getRelationships()
    {
        ArrayList<Relationship> rels = new ArrayList<Relationship>();
        for(DrawElement e : elements){
            if(e instanceof Relationship){                
                rels.add((Relationship)e);
            }
        }
        return rels;
    }
    
    public ArrayList<Relationship> getRelationshipsFrom(DiagramElement src)
    {
        ArrayList<Relationship> rels = new ArrayList<Relationship>();
        for(DrawElement e : elements){
            if(e instanceof Relationship){                
                Relationship r = (Relationship)e;
                if(r.getSource() == src)
                    rels.add(r);
            }
        }
        return rels;
    }
    
    public DiagramElement getElmentById(String id)
    {
        for(DrawElement e : elements){
            if(e instanceof DiagramElement){
                if(e.getId().equals(id))
                    return (DiagramElement)e;
            }
        }            
        return null;
    }
    
    public void removeElement(DiagramElement d)
    {
        if(this.elements.remove(d))
        {
            ArrayList<DrawElement> remove = new ArrayList<DrawElement>();
            ArrayList<Relationship> links = this.getRelationships();
            for(Relationship r : links)
            {
                if(r.getDestination()==d || r.getSource()==d)
                    remove.add(r);
            }
            //se elimina de las relaciones
            if(!remove.isEmpty())
                this.elements.removeAll(remove);
        }
    }
    
    public void removeRelationship(Relationship r){
        this.elements.remove(r);
    }
    
    //metodos para reordenar el pintado de los elementos
    public void sendToBack(DiagramElement e){
        elements.remove(e);
        elements.add(0, e);
    }
    
    public void bringToFront(DiagramElement e){
        elements.remove(e);
        elements.add(e);
    }
    
    
    
}
